package studentinformationmanagementsystem.servlet.admin;

import studentinformationmanagementsystem.entity.Course;
import studentinformationmanagementsystem.entity.Student;
import studentinformationmanagementsystem.service.CourseService;
import studentinformationmanagementsystem.service.StudentService;
import studentinformationmanagementsystem.service.impl.CourseServicelmpl;
import studentinformationmanagementsystem.service.impl.StudenServiceImpl;
import studentinformationmanagementsystem.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/admin/function/courses.do")
public class CourseServlet extends BaseServlet {
    CourseService courseService=new CourseServicelmpl();

    //查询所有的课程信息
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Course> courses = courseService.queryAll();
        request.setAttribute("courses",courses);
        //通过请求转发，将请求对象转发到课程列表jsp上
        request.getRequestDispatcher("/admin/function/subjects.jsp").forward(request,response);
    }

    //删除课程课表信息（根据课程id）
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer courseid = Integer.valueOf(request.getParameter( "courseid" ));
        Course course =new Course();
        course.setCourseid( courseid );
        int result = courseService.delete(course);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(String.valueOf(result));
    }

    //添加课程列表信息
     public void register(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取前端传回来的值（课程ID,课程名称，课程学分，学时，类别,课程编号）
         String coursename = request.getParameter( "coursename" );
         String coursecredit =request.getParameter( "coursecredit" );
         String period =request.getParameter( "period" );
         String type  =request.getParameter( "type" );
         String number = request.getParameter( "number" );
         int result = courseService.register(new Course(null,coursename,coursecredit,period,type,number,0));
         //直接跳转回课表查询界面
//         response.sendRedirect("courses.do?action=queryAll");
         response.setContentType("text/html;charset=utf-8");
         response.getWriter().write(String.valueOf(result));
     }
    //修改课程表信息
    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取前端传来的值（课程ID,课程名称，课程学分，学时，类别，课程编号）
        Integer courseid =null;
        if (request.getParameter( "courseid" ) != null){
            courseid = Integer.valueOf(request.getParameter( "courseid" ));
        }

        String coursename = request.getParameter( "coursename" );
        String coursecredit =request.getParameter( "coursecredit" );
        String period =request.getParameter( "period" );
        String type =request.getParameter( "type" );
        String number = request.getParameter( "number" );
        int result = courseService.update(new Course(courseid,coursename,coursecredit,period,type,number,0));
        //直接跳转回课表查询界面
//        response.sendRedirect("courses.do?action=queryAll");
        response.setContentType("text/html;charset=utf-8");
        if (result>0){
            response.getWriter().print(1);
        }
    }
    //判断课程名称是否重名
    public void isExists(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        String coursename = request.getParameter( "coursename" );
        Course course =new Course(  );
        course.setCoursename( coursename );
        //判断课程名称是否已存在 flag == true 已存在
        boolean flag = courseService.isExistsByCourseNname(course);
        PrintWriter writer = response.getWriter();
        if (flag) {
            //如果课程名称已存在
            writer.write("true");
        }
    }

    //判断课程编号是否重名
    public void isExistsNum(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        String number = request.getParameter( "number" );
        Course course =new Course(  );
        course.setNumber( number );
        //判断课程编号是否已存在 flag == true 已存在
        boolean flag1 = courseService.isExistsByNumber( course );
        PrintWriter writer = response.getWriter();
        if (flag1) {
            //如果课程名称已存在
            writer.write("true");
        }
    }

}
